package com.xiaomi.servlet;

import com.xiaomi.entity.Address;
import com.xiaomi.entity.User;
import com.xiaomi.service.AddressService;
import com.xiaomi.service.impl.AddressServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author jy
 * @date 2021/5/25
 */
@WebServlet("/address")
public class AddressServlet extends BaseServlet {

    private final AddressService addressService = new AddressServiceImpl();

    /**
     * 查询地址信息返回给self_info.jsp
     */
    protected String show(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        int uid = user.getUid();
        List<Address> addList = addressService.showAddressesByUid(uid);
        req.setAttribute("addList", addList);
        return "forward:/self_info.jsp";
    }

    /**
     * 添加地址信息
     */
    protected String add(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        Address address = new Address();
        try {
            BeanUtils.populate(address, map);
            address.setLevel(0);
            int i = addressService.addAddress(address);
            if (i > 0) {
                return "forward:/address?method=show";
            }
            req.setAttribute("msg", "添加地址失败");
            return "forward:/message.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("msg", "添加地址失败");
            return "forward:/message.jsp";
        }
    }

    /**
     * 删除地址信息
     */
    protected String delete(HttpServletRequest req, HttpServletResponse resp) {
        String aid = req.getParameter("aid");
        int count = addressService.delete(aid);
        if (count > 0) {
            return "forward:/address?method=show";
        }
        req.setAttribute("msg", "删除地址失败");
        return "forward:/message.jsp";
    }

    /**
     * 设置默认收货地址
     */
    protected String setDefault(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String aid = req.getParameter("aid");
        int uid = user.getUid();
        int count = addressService.setDefault(uid, aid);
        if (count > 0) {
            return "forward:/address?method=show";
        }
        req.setAttribute("msg", "设置默认地址失败");
        return "forward:/message.jsp";
    }

    protected String update(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        Address address = new Address();
        try {
            BeanUtils.populate(address, map);
            int count = addressService.update(address);
            if (count > 0) {
                return "forward:/address?method=show";
            }
            req.setAttribute("msg", "设置默认地址失败");
            return "forward:/message.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("msg", "设置默认地址失败");
            return "forward:/message.jsp";
        }
    }
}
